package com.yaowk.common.generator;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.generator.BaseModelGenerator;
import com.jfinal.plugin.activerecord.generator.ModelGenerator;
import com.yaowk.common.kit.DataPluginKit;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;

/**
 * model生成工具抽象类
 * <p>
 * yaowk
 * 2017/6/27
 */
public abstract class AbstractDefaultGenerator {

    private List<String> tableNames;
    private String baseModelPackageName;
    private String baseModelOutputDir;
    private String modelPackageName;
    private String modelOutputDir;
    private String tableNamePrefix;
    private ModelGenerator modelGenerator;
    private BaseModelGenerator baseModelGenerator;

    public AbstractDefaultGenerator(String[] tableNames, String tableNamePrefix) {
        this.tableNames = Arrays.asList(tableNames);
        this.modelPackageName = this.getClass().getPackage().getName().replace("common", "model");
        this.modelOutputDir = PathKit.getWebRootPath() + "/src/main/java/" + modelPackageName.replaceAll("[.]", "/");
        this.baseModelPackageName = modelPackageName + ".base";
        this.baseModelOutputDir = modelOutputDir + "/base";
        this.tableNamePrefix = tableNamePrefix;
        this.modelGenerator = new DefaultModelGenerator(modelPackageName, baseModelPackageName, modelOutputDir);
        this.baseModelGenerator = new BaseModelGenerator(baseModelPackageName, baseModelOutputDir);
    }

    public AbstractDefaultGenerator(String[] tableNames) {
        this(tableNames, null);
    }

    public void generator() {
        // 创建生成器
        DataSource dataSource = DataPluginKit.getDruidDataSource();
        com.jfinal.plugin.activerecord.generator.Generator generator = new com.jfinal.plugin.activerecord.generator.Generator(dataSource, baseModelGenerator, modelGenerator);
        generator.setDialect(new MysqlDialect());
        generator.setMetaBuilder(new BaseMetaBuilder(dataSource, tableNames));
        generator.setGenerateChainSetter(true);
        // 设置是否在 Model 中生成 dao 对象
        generator.setGenerateDaoInModel(true);
        // 设置是否生成字典文件
        generator.setGenerateDataDictionary(false);
        // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user"，移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
        if (StrKit.notBlank(tableNamePrefix)) {
            generator.setRemovedTableNamePrefixes(tableNamePrefix);
        }

        // 生成
        generator.generate();
    }

    public void setModelGenerator(ModelGenerator modelGenerator) {
        this.modelGenerator = modelGenerator;
    }

    public void setBaseModelGenerator(BaseModelGenerator baseModelGenerator) {
        this.baseModelGenerator = baseModelGenerator;
    }

    public List<String> getTableNames() {
        return tableNames;
    }

    void setTableNames(List<String> tableNames) {
        this.tableNames = tableNames;
    }

    public String getBaseModelPackageName() {
        return baseModelPackageName;
    }

    public void setBaseModelPackageName(String baseModelPackageName) {
        this.baseModelPackageName = baseModelPackageName;
    }

    public String getBaseModelOutputDir() {
        return baseModelOutputDir;
    }

    void setBaseModelOutputDir(String baseModelOutputDir) {
        this.baseModelOutputDir = baseModelOutputDir;
    }

    public String getModelPackageName() {
        return modelPackageName;
    }

    public void setModelPackageName(String modelPackageName) {
        this.modelPackageName = modelPackageName;
    }

    public String getModelOutputDir() {
        return modelOutputDir;
    }

    public void setModelOutputDir(String modelOutputDir) {
        this.modelOutputDir = modelOutputDir;
    }

    public String getTableNamePrefix() {
        return tableNamePrefix;
    }

    public void setTableNamePrefix(String tableNamePrefix) {
        this.tableNamePrefix = tableNamePrefix;
    }

    public ModelGenerator getModelGenerator() {
        return modelGenerator;
    }

    public BaseModelGenerator getBaseModelGenerator() {
        return baseModelGenerator;
    }
}
